/**
* @class
* Works with a bizproc to show a list of bizobjs using templates and the templating engine
* available in the app framework.
* @meta-type jQuery widget
* @Depends ksg.baseWidget.js
*/
ksg.ui.widgets.BizObjListDisplay = $.extend({},ksg.ui.widgets.BaseWidget, /** @lends ksg.ui.widgets.BizObjListDisplay# */{
	options: {
	},
	
	_create: function() { var self = this; var $this = this.element;
		ksg.App.bizProcMgr.registerWidget(this);
		$this.find('.ksg-ajax-nav').live('click', function(event) {
			var id = null;
			var a = $(this);
			while((a = a.parent())[0] != $this[0])
				if(id = a.data('bizobjid'))
					break;

			//alert("would have navigated!:" + id);
			if(id) {
				var bizProc = ksg.App.bizProcMgr.getBizProc(self.getParentBizProcId());
				if(bizProc) {
					bizProc.selectBizObj(id);
					event.preventDefault();
				}
			}
		});
	},
	
	destroy: function() {
		ksg.App.bizProcMgr.unregisterWidget(this);
	},
	
	//--- IWidget	
	getWidgetType: function() {return ksg.ui.BizProcWidgetType.List;},
	
	//--- The following are required IBizObjListDisplay interface methods
	DrivenByBizProc: function(){return true;},
	
	setSelectedBizObjKeyValue: function(val) {
		this.selectedBizObjKeyValue = val;
		this.element.find(".ksg-selected").removeClass("ksg-selected");
		this.element.find("[data-bizobjid='"+val+"']").addClass("ksg-selected");
	},
	
	showBizObjs: function(bizProc, bizObjs) {
		var self = this;
		ksg.ui.getTemplate(self._getTemplateName(), "", function(json) {
			self.element.html("");
			json.tmpl.tmpl(bizObjs.pushForward(self.MaxElements())).appendTo(self.element);
			self.setSelectedBizObjKeyValue(self.selectedBizObjKeyValue);
            self.element.trigger("tmplApplied");
		});
	},
	
	MaxElements: function() {
		var max = this.options.maxElements;
		return (!(max))?-1:max;
	}
	
});
$.widget("ksg.bizObjListDisplay", ksg.ui.widgets.BizObjListDisplay);

ksg.ui.widgetMap["ksg-bizObjListDisplay"] = function ($el, options) {
	$el.bizObjListDisplay(options); 
};
